<?php

require_once($_SERVER["DOCUMENT_ROOT"] . '/Esquina_da_TI/ClassesBasicas/Enquete.php');
require_once($_SERVER["DOCUMENT_ROOT"] . '/Esquina_da_TI/DAO/Conexao.php');

$conexao = new Conexao();


$enquete = null;

$query = "SELECT enquete.titulo, enquete.opcao1, enquete.opcao2, enquete.opcao3,
                    enquete.votos1, enquete.votos2, enquete.votos3 from enquete where estado = '".EstadoEnquete::APURANDO."'";


$conexao->abrirConexao();

$result = $conexao->executarQuery($query);

if(mysql_num_rows($result) != 0) {
    $e = mysql_fetch_array($result);

    $enquete = new Enquete();
    $enquete->setTitulo($e['titulo']);
    $enquete->setOpcao1($e['opcao1']);
    $enquete->setOpcao2($e['opcao2']);
    $enquete->setOpcao3($e['opcao3']);
    $enquete->setVotos1($e['votos1']);
    $enquete->setVotos2($e['votos2']);
    $enquete->setVotos3($e['votos3']);
}

?>

<!--
To change this template, choose Tools | Templates
and open the template in the editor.
-->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <script type="text/javascript" src="formulario.js"></script>
        <link href="../default.css" rel="stylesheet" type="text/css" media="screen" />
        
        <title>Resultado Parcial</title>
    </head>
    <body>
        <h4>Resultado Parcial</h4>

        <script type="text/javascript">

        <?php
        echo "var opcao1 = '" . $enquete->getOpcao1() . "';";
        echo "var opcao2 = '" . $enquete->getOpcao2() . "';";
        echo "var opcao3 = '" . $enquete->getOpcao3() . "';";

        echo "var votos1 = " . $enquete->getVotos1() . ";";
        echo "var votos2 = " . $enquete->getVotos2() . ";";
        echo "var votos3 = " . $enquete->getVotos3() . ";";
        ?>

    var graphimage="/Esquina_da_TI/images/poll.JPG";

    //DEFINE GRAPH VALUES [Item name, absolute value]
    var graphx=new Array()
    graphx[0]=[opcao1,votos1]
    graphx[1]=[opcao2,votos2]
    graphx[2]=[opcao3,votos3]


    //YOU CAN DEFINE MULTIPLE GRAPHS, eg:
    //var graphy=new Array()


    function graphit(g,gwidth){
        total=0
        for (i=0;i<g.length;i++)
            total+=parseInt(g[i][1])

        output='<table border="0" cellspacing="0" cellpadding="0">'
        for (i=0;i<g.length;i++){
            calpercentage=Math.round(g[i][1]*100/total)
            calwidth=Math.round(gwidth*(calpercentage/100))
            output+='<tr><td>'+g[i][0]+'&nbsp;</td><td><img src="'+graphimage+'" width="'+calwidth+'" height="10"> '+calpercentage+'%</td></tr>'
        }
        output+='</table>'
        document.write(output+'<br>Total de Votos: <b>'+total+'</b>')
    }

    //CALL GRAPHIT FUNCTION
    //graphit(NAME OF GRAPH ARRAY, MAXIMUM WIDTH IN PIXELS)
    graphit(graphx,200)

        </script>
    </body>
</html>
